package com.lizardmind.haofudoctor.manager

import android.os.Environment
import android.widget.ImageView
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.linxiao.framework.BaseApplication
import com.linxiao.framework.glide.GlideApp

/**
 *
 * Created by Extends on 2017/10/23 14:47
 */
object ImgManager {
    var IMGURL = "http://doctor.admin.lizardmind.com"
    var ERROR = android.R.mipmap.sym_def_app_icon//错误图
    var PLACEHOLDER = android.R.mipmap.sym_def_app_icon//占位图

    private val defaultRequestOption by lazy {
        RequestOptions().placeholder(PLACEHOLDER)
                .error(ERROR)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
    }

    fun showImg(url:String?, image:ImageView, ro:RequestOptions = defaultRequestOption){
        if(url == null)return
        if(url.isEmpty())return

        // 判断url不是全路径，则加上IMGURL
        val urls = if(url.startsWith("/data/")
                ||url.startsWith(Environment.getExternalStorageDirectory().path)
                || url.startsWith("/storage/")){
            url
        }else if(!url.startsWith("http")){
            IMGURL+url
        }else url

        GlideApp.with(BaseApplication.getAppContext())
                .load(urls)
                .apply(ro)
                .into(image)
    }
}

/**
 * 给ImageView设置图片
 */
fun ImageView.setImageUrl(url:String?){
    ImgManager.showImg(url,this)
}